package network;

import android.content.Context;


import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;

import org.apache.http.HttpEntity;


/**
 * Created by renkequan on 2017/3/14.
 */

//http接口
public class NetWork {
//    private static final String BASE_URL = "http://10.136.3.88:7080/app-ljyyx/rest/app/";
    private static final String BASE_URL = "http://192.168.0.124:8080/app-ljyyx/rest/app/";
//    public static final String BASE_URL = "https://www.st12306.cn/xproxy/yproxy/ljyyx/rest/app/";

    public static AsyncHttpClient client;

    public static AsyncHttpClient getInstance(){
        if(client==null){
            client=new AsyncHttpClient();
            client.setTimeout(20000);
            client.setMaxRetriesAndTimeout(2,20000);
            client.addHeader("Authorization","Basic TEpZWVg6TEpZWVhAMTIzNA==");
        }
        return client;
    }


    public static void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        getInstance().get(context, getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        getInstance().post(context, getAbsoluteUrl(url), params, responseHandler);
    }

    public static void postNoAbsoluterUrl(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        getInstance().post(context, url, params, responseHandler);
    }

    public static void post(Context context, String url, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
        getInstance().post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);

    }


    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}
